<?php

namespace App\Http\Services\FormBuilder\Elements;

class Checkbox extends Input
{
    private $checked;
    public $fillable = [
        'id','type' , 'class', 'value','name','checked'
    ];

    public function render()
    {
        if ($this->label) {
            $element        = clone $this;
            $element->label = false;

            return (new Field(new CheckboxWrapper($element, $this->label)))->render();
        }

        $result = '<input';

        $result .= $this->renderAttributes();

        $result .= '>';

        return $result;
    }

    public function defaultToChecked()
    {
        if (!isset($this->checked)) {
            $this->check();
        }

        return $this;
    }

    public function defaultToUnchecked()
    {
        if (!isset($this->checked)) {
            $this->uncheck();
        }

        return $this;
    }

    public function defaultCheckedState($state)
    {
        $state ? $this->defaultToChecked() : $this->defaultToUnchecked();

        return $this;
    }

    public function check()
    {
        $this->setChecked(true);

        return $this;
    }

    public function uncheck()
    {
        $this->setChecked(false);

        return $this;
    }

    protected function setChecked($checked = true)
    {
        $this->checked = $checked;
        $this->removeAttribute('checked');

        if ($checked) {
            $this->setAttribute('checked', 'checked');
        }
    }

    public function editBlock()
    {
        //dd($this);
        $outLabel = (new Label($this->getAttribute('display_name')))
            ->addClass('d-block')
            ->forId($this->getAttribute('id'))
            ->required((int) $this->getAttribute('require'));

        $tips   = (new HelpBlock($this->getAttribute('tips')));
        $fields = '';
        $value = $this->getAttribute('value');
        foreach ($this->getAttribute('options') as $key=>$v)
        {
            $label = (new Label($v))
                ->addClass('custom-control-label font-w400')
                ->forId('checkbox-custom-inline1'.$key);

            if($value){
                if (in_array($key,$value)) {
                    $this->setAttribute('checked',true);
                } else {
                    $this->removeAttribute('checked');
                }
            }
            $this->setAttribute('id','checkbox-custom-inline1'.$key);
            $this->setAttribute('class','custom-control-input');
            $this->setValue($key);
            $div = (new Field($this,$label))
                ->addClass('custom-control custom-checkbox custom-control-inline custom-control-primary');
            $div->setAttribute('style','margin-top: 5px;');
            $fields .= $div->render();
        }

        $li_class = $this->getAttribute('li_class');
        $fieldName = $this->getAttribute('name');
        //hidden 字段 防止数据为空时，post后台收不到该字段
        $html = '<div class="'.$li_class.'" style="padding: 10px;">';
        $html .= '<input type="hidden" name="'.$fieldName.'" value="">';
        $html .= $outLabel;
        $html .= $fields;
        $html .= $tips;
        $html .= '</div>';

        return $html;
    }

}
